using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Runtime.CompilerServices;

namespace Catastrophe.Common.ObjM
{
	public class ControlLogic<T> where T : IComparable
	{
		public Queue<T> QValueBuf = new Queue<T>();

		private bool _firstFlag = true;

		public object LastValue { get; private set; }

		public ControlLogic()
		{
		}

		public ControlLogic(T t)
		{
			this.LastValue = t;
		}

		public bool IsDataChange(T value, T[] condition = null, Predicate<T> firstCondition = null, int defaultStoreLength = 10)
		{
			return this.IsDataChangePredicate(value, condition, firstCondition, defaultStoreLength).Item1;
		}

		public Tuple<bool, T, object> IsDataChangePredicate(T value, T[] condition = null, Predicate<T> firstCondition = null, int defaultStoreLength = 10)
		{
			Tuple<bool, T, object> result = new Tuple<bool, T, object>(item1: true, value, this.LastValue);
			Tuple<bool, T, object> result2 = new Tuple<bool, T, object>(item1: false, value, this.LastValue);
			if (this._firstFlag)
			{
				this._firstFlag = false;
				try
				{
					if (firstCondition != null)
					{
						this.QueueUpdate(value, defaultStoreLength);
						if (firstCondition(value))
						{
							return result;
						}
						return result2;
					}
				}
				catch
				{
					this.QueueUpdate(value, defaultStoreLength);
				}
			}
			if (condition?.Contains(value) ?? false)
			{
				this.QueueUpdate(value, defaultStoreLength);
				return result2;
			}
			if (value == null)
			{
				this.QueueUpdate(null, defaultStoreLength);
				if (this.LastValue == null)
				{
					return result2;
				}
				return result;
			}
			if (value.CompareTo(this.LastValue) != 0)
			{
				this.QueueUpdate(value, defaultStoreLength);
				return result;
			}
			return result2;
		}

		private void QueueUpdate(object value, int defaultStoreLength)
		{
			this.LastValue = value;
			if (this.QValueBuf.Count < defaultStoreLength)
			{
				this.QValueBuf.Enqueue((T)value);
			}
			else
			{
				this.QValueBuf.Dequeue();
			}
		}

		public void LastDataIni()
		{
			this.LastValue = null;
			this.QValueBuf.Clear();
		}

		public bool IsMatch(float[] mpValueBuf, string condition, int beforeOperator = 4, string keyWord = "MP")
		{
			DataTable dataTable = new DataTable();
			dataTable.Columns.Add("Name");
			dataTable.Columns.Add("Operator");
			dataTable.Columns.Add("LimitValue");
			DataTable dataTable2 = new DataTable();
			dataTable2.Columns.Add("Index", typeof(int));
			dataTable2.Columns.Add("Name");
			dataTable2.Columns.Add("Operator");
			dataTable2.Columns.Add("LimitValue");
			char[] separator = new char[2] { '|', '|' };
			char[] separator2 = new char[2] { '&', '&' };
			string[] array = condition.Split(separator, StringSplitOptions.RemoveEmptyEntries);
			for (int i = 0; i < array.Length; i++)
			{
				if (array[i].Contains("&&"))
				{
					string[] array2 = array[i].Split(separator2, StringSplitOptions.RemoveEmptyEntries);
					string[] array3 = array2;
					foreach (string text in array3)
					{
						DataRow dataRow = dataTable2.NewRow();
						dataRow["Index"] = i.ToString();
						dataRow["Name"] = text.Substring(0, 4).Trim();
						dataRow["Operator"] = text.Substring(beforeOperator, 1).Trim();
						dataRow["LimitValue"] = text.Substring(beforeOperator + 1, text.Length - 5).Trim();
						dataTable2.Rows.Add(dataRow);
					}
				}
				else
				{
					DataRow dataRow2 = dataTable.NewRow();
					dataRow2["Name"] = array[i].Substring(0, 4).Trim();
					dataRow2["Operator"] = array[i].Substring(beforeOperator, 1).Trim();
					dataRow2["LimitValue"] = array[i].Substring(beforeOperator + 1, array[i].Length - 5).Trim();
					dataTable.Rows.Add(dataRow2);
				}
			}
			List<string> list = new List<string>();
			for (int k = 0; k < mpValueBuf.Length; k++)
			{
				DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(0, 2);
				defaultInterpolatedStringHandler.AppendFormatted(keyWord);
				defaultInterpolatedStringHandler.AppendFormatted(k + 1, "D2");
				string text2 = defaultInterpolatedStringHandler.ToStringAndClear();
				DataRow[] array4 = dataTable.Select("[Name]='" + text2 + "'");
				if (list.Contains(text2))
				{
					continue;
				}
				string text3 = "";
				float result;
				if (array4.Length == 0)
				{
					defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(0, 2);
					defaultInterpolatedStringHandler.AppendFormatted(keyWord);
					defaultInterpolatedStringHandler.AppendFormatted(k + 1, "D2");
					DataRow[] array5 = dataTable2.Select("[Name]='" + defaultInterpolatedStringHandler.ToStringAndClear() + "'");
					if (array5.Length == 0)
					{
						continue;
					}
					DataRow[] array6 = dataTable2.Select("[Index]='" + (int)array5[0]["Index"] + "'");
					bool flag = false;
					DataRow[] array7 = array6;
					foreach (DataRow dataRow3 in array7)
					{
						string item = dataRow3["Name"].ToString()!.Trim();
						list.Add(item);
						if (!flag)
						{
							text3 = dataRow3["Operator"].ToString()!.Trim();
							if (!float.TryParse(dataRow3["LimitValue"].ToString()!.Trim(), out result) || this.CompareLimit(text3, mpValueBuf[k], result))
							{
								return true;
							}
							flag = true;
						}
					}
				}
				else
				{
					text3 = array4[0]["Operator"].ToString()!.Trim();
					if (float.TryParse(array4[0]["LimitValue"].ToString()!.Trim(), out result) && this.CompareLimit(text3, mpValueBuf[k], result))
					{
						return true;
					}
				}
			}
			return false;
		}

		private bool CompareLimit(string opr, float valueNow, float limitValue)
		{
			bool result = false;
			switch (opr)
			{
			case "<":
				result = valueNow < limitValue;
				break;
			case "<=":
				result = valueNow <= limitValue;
				break;
			case ">":
				result = valueNow > limitValue;
				break;
			case ">=":
				result = valueNow >= limitValue;
				break;
			case "=":
				result = valueNow.CompareTo(limitValue) == 0;
				break;
			}
			return result;
		}
	}
}
